<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单总额" prop="sum">
        <el-input
          v-model="queryParams.sum"
          placeholder="请输入订单总额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="顾客姓名" prop="customer">
        <el-input
          v-model="queryParams.customer"
          placeholder="请输入顾客姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orders:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orders:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="ordId" />
      <el-table-column label="总额/CNY" align="center" prop="sum" />
      <el-table-column label="顾客姓名" align="center" prop="customer" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleCheck(scope.row)">查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['orders:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <p>订单号：{{orderInfo.ordId}}</p>
      <p>顾客：{{orderInfo.customer}}</p>
      <p>总金额：{{orderInfo.sum}} CNY</p>
      <el-card shadow="never" v-for="item in orderInfo.clothList" :key="item.ordId" class="card">
        <div slot="header">商品名：{{item.name}}  |  单价：{{item.price}}CNY  |  数量:{{item.sum}}</div>
        <div>种类：{{item.classify}}    |   尺码：{{item.clonthSize}}   |   颜色：{{item.color}}</div>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, delOrder, exportOrder } from '@/api/orders/order'

export default {
  name: 'Order',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      orderList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clothList: null,
        sum: null,
        customer: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clothList: [
          { required: true, message: '商品列表不能为空', trigger: 'blur' }
        ],
        sum: [{ required: true, message: '订单总额不能为空', trigger: 'blur' }]
      },
      // 订单信息
      orderInfo: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        ordId: null,
        clothList: null,
        sum: null,
        customer: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 查看详细信息
    handleCheck(row) {
      this.open = true;
      this.orderInfo = row;
      this.title = '查看订单信息';
      this.orderInfo.clothList = JSON.parse(this.orderInfo.clothList)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.ordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ordIds = row.ordId || this.ids
      this.$confirm(
        '是否确认删除订单信息编号为"' + ordIds + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delOrder(ordIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有订单信息数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return exportOrder(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
    }
  }
}
</script>

<style lang="scss">
  .card{
    margin-bottom: 10px;
  }
</style>